package com.bjx.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.common.core.exception.BJXException;
import com.bjx.system.domain.entity.DictTypeEntity;
import com.bjx.system.domain.query.DictTypeQuery;
import com.bjx.api.system.domain.vo.DictDataVO;
import com.bjx.system.domain.vo.DictTypeVO;
import com.bjx.system.mapper.DictTypeMapper;
import com.bjx.system.service.IDictDataService;
import com.bjx.system.service.IDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @desc: 字典类型业务实现类
 * @author: liushuai
 * @date: 2022/8/15 14:30
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictTypeEntity> implements IDictTypeService {
    @Autowired
    private DictTypeMapper dictTypeMapper;
    @Autowired
    private IDictDataService dictDataService;

    @Override
    public IPage<DictTypeVO> listDictTypePage(DictTypeQuery dictTypeQuery) {
        IPage<DictTypeVO> iPage = dictTypeMapper.listDictTypePage(dictTypeQuery.getPage(),dictTypeQuery);
        return iPage;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertDictType(DictTypeVO dictTypeVO) {
        String dictTypeCode = dictTypeVO.getDictTypeCode();
        DictTypeEntity dictTypeEntity = selectDictTypeByDictTypeCode(dictTypeCode);
        if(ObjectUtil.isNotNull(dictTypeEntity)){
            throw new BJXException("字典类型已存在");
        }
        DictTypeEntity entity = Convert.convert(DictTypeEntity.class, dictTypeVO);
        dictTypeMapper.insert(entity);
        //保存字典
        dictDataService.saveDictData(dictTypeVO.getDict(),dictTypeVO.getDictTypeCode());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDictType(DictTypeVO dictTypeVO) {
        String dictTypeCode = dictTypeVO.getDictTypeCode();
        DictTypeEntity dictTypeEntity = selectDictTypeByDictTypeCode(dictTypeCode);
        if(ObjectUtil.isNotNull(dictTypeEntity) && !String.valueOf(dictTypeEntity.getDictTypeId()).equals(dictTypeVO.getDictTypeId())){
            throw new BJXException("字典类型已存在");
        }
        DictTypeEntity entity = Convert.convert(DictTypeEntity.class, dictTypeVO);
        dictTypeMapper.updateById(entity);
        //保存字典
        dictDataService.saveDictData(dictTypeVO.getDict(),dictTypeVO.getDictTypeCode());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteDictTypeByDictTypeId(Long dictTypeId) {
        DictTypeEntity entity = dictTypeMapper.selectById(dictTypeId);
        dictTypeMapper.deleteById(dictTypeId);
        dictDataService.deleteDictDataByDictTypeCode(entity.getDictTypeCode());
    }

    @Override
    public DictTypeVO selectDictTypeByDictTypeId(Long dictTypeId) {
        DictTypeEntity entity = dictTypeMapper.selectById(dictTypeId);
        DictTypeVO vo = Convert.convert(DictTypeVO.class,entity);
        List<DictDataVO> dictDataVOS = dictDataService.listDictDataByDictTypeCode(vo.getDictTypeCode());
        vo.setDict(dictDataVOS);
        return vo;
    }


    public DictTypeEntity selectDictTypeByDictTypeCode(String dictTypeCode){
        LambdaQueryWrapper<DictTypeEntity> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StrUtil.isNotEmpty(dictTypeCode),DictTypeEntity::getDictTypeCode,dictTypeCode);
        return dictTypeMapper.selectOne(wrapper);
    }


}
